package com.funquer.cms.service.impl;

import com.funquer.admin.pageModel.DataGrid;
import com.funquer.cms.model.ArctypeEntity;
import com.funquer.cms.pageModel.ArctypePage;
import com.funquer.cms.service.ArctypeServiceI;
import com.funquer.dao.BaseDaoI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-17
 * Time: 下午12:24
 * To change this template use File | Settings | File Templates.
 */
@Service("arctypeService")
public class ArctypeServiceImpl implements ArctypeServiceI {


    private BaseDaoI<ArctypeEntity> arctypeDao;

    public BaseDaoI<ArctypeEntity> getArctypeDao() {
        return arctypeDao;
    }

    @Autowired
    public void setArctypeDao(BaseDaoI<ArctypeEntity> arctypeDao) {
        this.arctypeDao = arctypeDao;
    }

    @Override
    public DataGrid datagrid(ArctypePage arctypePage) {
        /*DataGrid dg = new DataGrid();
        String hql="FROM ArctypeEntity";
        Map<String, Object> params = new HashMap<String, Object>();
        hql = addWhere(arctypePage, hql, params);
        String totalHql = "select count(*) FROM ArctypeEntity";
        Map<String, Object> params2 = new HashMap<String, Object>();
        totalHql = addWhere(arctypePage, totalHql, params2);
        hql = addOrder(arctypePage, hql);
        List<ArctypeEntity> l = arctypeDao.find(hql, params, arctypePage.getPage(), arctypePage.getRows());
        dg.setTotal(arctypeDao.count(totalHql, params));
        dg.setRows(l);
        return dg;*/
        return null;
    }

    @Override
    public List<ArctypePage> treegrid() {
        List<ArctypeEntity> l = arctypeDao.find("from ArctypeEntity t order by t.sortrank");
        List<ArctypePage> nl = new ArrayList<ArctypePage>();
        if (l != null && l.size() > 0) {
            for (ArctypeEntity t : l) {
                ArctypePage r = new ArctypePage();
                if (t.getParentArcType() != null) {
                    r.setPid(t.getParentArcType().getId());
                    r.setPname(t.getParentArcType().getTypeName());
                }
                r.setId(t.getId());
                r.setText(t.getTypeName());
                nl.add(r);
            }
        }
        return nl;
    }


    /*private String addOrder(ArctypePage arctypePage, String hql) {
        if (arctypePage.getSort() != null) {
            hql += " order by " + arctypePage.getSort() + " " + arctypePage.getOrder();
        }
        return hql;
    }

    private String addWhere(ArctypePage arctypePage, String hql, Map<String, Object> params) {
        hql += " where 1=1 ";
        if (arctypePage.getChannelTypeName() != null && !arctypePage.getChannelTypeName().trim().equals("")) {
            hql += " and typename like '%%" + arctypePage.getChannelTypeName().trim() + "%%'";
        }
        return hql;
    }*/

    @Override
    public ArctypeEntity save(ArctypeEntity arctypeEntity) {
        if(arctypeEntity!=null){
            arctypeEntity.setId(UUID.randomUUID().toString());
        }
        arctypeDao.save(arctypeEntity);
        return arctypeEntity;
    }

    @Override
    public void remove(String ids) {
        for (String id : ids.split(",")) {
            ArctypeEntity t = arctypeDao.get(ArctypeEntity.class, id);
            if (t != null) {
                arctypeDao.delete(t);
            }
        }
    }

    @Override
    public ArctypeEntity getOne(String id) {
        ArctypeEntity t = arctypeDao.get(ArctypeEntity.class, id);
        if (t != null) {
            return t;
        }
        return null;
    }

    @Override
    public ArctypeEntity edit(ArctypeEntity ArctypeEntity) {
        arctypeDao.update(ArctypeEntity);
        return ArctypeEntity;
    }
}
